<template>
  <div class="saveUpdate">
    <el-dialog :close-on-click-modal="false" :title="id?'编辑商户':'新增商户'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="500px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="150px" size="mini">
        <dialog-wrapper :MaxWidth="600">
          <el-form-item label="商户名称：" prop="name" :rules="[{ required: true, message: '商户名称不能为空'}]">
            <el-input v-model="DetailInfo.name" :disabled="id?true:false" placeholder="请输入商户名称..."></el-input>
          </el-form-item>
          <el-form-item v-if="!id" label="商户类型：" prop="mch_type" :rules="[{ required: true, message: '商户类型不能为空'}]">
            <el-select v-model="DetailInfo.mch_type">
              <el-option v-for="(item, key) of MchType" :key="key" :label="item"
                :value="key" ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="订单地址：" prop="create_order_url" :rules="[{ required: true, message: '订单地址不能为空'}]">
            <el-input v-model="DetailInfo.create_order_url"
            ></el-input>
          </el-form-item>
          <el-form-item label="充值地址：" prop="recharge_order_url" :rules="[{ required: true, message: '充值地址不能为空'}]">
            <el-input v-model="DetailInfo.recharge_order_url"
            ></el-input>
          </el-form-item>
          <el-form-item label="追分地址：" prop="chase_order_url" :rules="[{ required: true, message: '追分地址不能为空'}]">
            <el-input v-model="DetailInfo.chase_order_url" />
          </el-form-item>
          <el-form-item label="充值查询地址：" prop="get_charge_info_url" :rules="[{ required: true, message: '充值信息查询地址不能为空'}]">
            <el-input v-model="DetailInfo.get_charge_info_url" />
          </el-form-item>
          <el-form-item label="订单失败地址：" prop="fail_order_url" :rules="[{ required: true, message: '订单失败地址不能为空'}]">
            <el-input v-model="DetailInfo.fail_order_url"
            ></el-input>
          </el-form-item>
          <el-form-item label="商户状态：">
            <el-switch v-model="DetailInfo.enable"></el-switch>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import {getMerchantsDetail, saveMerchants, updateMerchant} from '@/api'
  import {useVcode} from "../../utils/CommonJs";

  export default {
    components: {DialogWrapper},
    data() {
      return {
        setbank: false,
        showDialog: this.showUpdate,
        MchType: this.$store.state.app.MchType,
        loading: false,
        DetailInfo: {
          enable:true
        }
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      id: {
        type: Number
      },
      MerchantType: {
        type: Object
      },
      settleCycle: {
        type: Object
      },
    },
    created() {
      if (this.id) {
        this.getDetail()
      }
    },
    methods: {
      saveDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            useVcode(this,()=>{
              saveMerchants(this.DetailInfo).then(res => {
                if (res.status === 0) {
                  this.$message({message: '新增成功。', type: 'success'})
                  this.$emit('update:showUpdate', false)
                }
              })
            })
          }
        })
      },
      updateDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            useVcode(this,()=> {
              updateMerchant(this.DetailInfo, this.id).then(res => {
                if (res.status === 0) {
                  this.$message({message: '编辑成功。', type: 'success'})
                  this.$emit('update:showUpdate', false)
                }
              })
            })
          }
        })
      },
      getDetail() {
        getMerchantsDetail(this.id).then(res => {
          if (res.status === 0) {
            this.DetailInfo = res.data
          }
        })
      }
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>

